package com.example.k8s.hello.service.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * get请求中对象的序列化
 */
@Configuration
public class ConverterConfig {
    @Bean
    public Converter<String, Date> localDateTimeConverter() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                LocalDateTime time = LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                return new Date(time.toEpochSecond(ZoneOffset.ofHours(8)));
            }
        };
    }
}
